package com.tenglong.teacher.ui.homework;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.homework.HomeWorkStateItem;
import com.tenglong.teacher.ui.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HomeWorkStateAdapter extends BaseAdapter {
    private Map<String,List<HomeWorkStateItem>> data;
    private Context context;
    private List<String> keys;
    public HomeWorkStateAdapter(Context _context,Map<String,List<HomeWorkStateItem>> _data) {
        this.context = _context;
        this.data = _data;
        keys = new ArrayList<>();
        for(String key: _data.keySet()){
            this.keys.add(key);
        }

    }
    @Override
    public int getCount() {
        return data.size();
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_state,null);
        TextView nameView = view.findViewById(R.id.tv_week_day);
        TextView numView =  view.findViewById(R.id.tv_submit_num);
        nameView.setText("周四");
        numView.setText("12/18");
        LinearLayout linearLayout = view.findViewById(R.id.ll_content);
        Log.i("cuotibao","data.size="+data.size()+" position"+position);
        List<HomeWorkStateItem> array = data.get(keys.get(position));
        if(array == null){
            Log.i("cuotibao","data.size="+data.size()+" is null :position"+position);
        }

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 3, 10, 3);
        int count = data.get(keys.get(position)).size();
        int lines = count / 4;
        int submitnum = 0;
        if(data.get(keys.get(position)).size() % 4 > 0){
            lines = lines + 1;
        }
        Log.i("cuotibao","lines="+lines);
        Log.i("cuotibao","length="+count);

        for(int i = 0;i < lines;i++){

            LinearLayout horizLL = new LinearLayout(context);
            horizLL.setOrientation(LinearLayout.HORIZONTAL);
            horizLL.setLayoutParams(layoutParams);
            LinearLayout.LayoutParams horizLLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            horizLLParams.setMargins(10, 3, 10, 3);

            for(int k=0;k < 4;k++) {//根据snValue数组的长度动态加载TextView
                if((i*4 +k) < count){
                    HomeWorkStateItem localitem = data.get(keys.get(position)).get(i*4 +k);
                    int bgColorID = ContextCompat.getColor(context,R.color.mygrey);
                    int txtcolorID = R.drawable.submit_no;

                    if(localitem.getStatus()==  MyUtils.HW_STATUS_SUBMIT){
                        bgColorID  = ContextCompat.getColor(context,R.color.theme_green);
                        txtcolorID = R.drawable.submit_yes;
                        submitnum ++;
                    }
                    TextView valueView = new TextView(context);
                    valueView.setText(localitem.getStudentName());
                    valueView.setTextColor(bgColorID);
                    valueView.setLayoutParams(layoutParams);
                    valueView.setBackgroundResource(txtcolorID);
                    valueView.setPadding(20,20,20,20);
                    horizLL.addView(valueView);
                }else{
                    break;
                }

            }

            linearLayout.addView(horizLL);
            numView.setText(String.format("%d/%d",submitnum,count));//MyUtils.dateToWeek(
            String time = MyUtils.getDateTimeByFormat(data.get(keys.get(position)).get(0).getCreateDate(),"YYYY/MM/dd ");
            nameView.setText(time + MyUtils.dateToWeek(time));
        }
//        for(int i=0;i< data.get(keys.get(position)).length;i++) {//根据snValue数组的长度动态加载TextView
//            TextView valueView = new TextView(context);
//            valueView.setText(data.get(keys.get(position))[i]);
//            valueView.setTextColor(context.getColor(R.color.theme_green));
//            valueView.setLayoutParams(layoutParams);
//            valueView.setBackgroundResource(R.drawable.submit_yes);
//            valueView.setPadding(20,20,20,20);
//            linearLayout.addView(valueView);
//        }
        return view;
    }
    public long getItemId(int position) {
        return 0;
    }
    public Object getItem(int position) {
        return null;
    }
}